package net.sunflower.demo;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;

/**
 * java导出PDF
 * 
 * @author wangyong
 */
public class ItextExpoPDF {
	private static final String FILE_DIR = null;

	public static void main(String[] args) throws Exception {
		String pdfPath = "F://w3schoolSoft/科协文档/iText-hello.pdf";
		// createPdf(pdfPath);
		createPDf(pdfPath, "yh", "3wsoft");

	}

	public static void main1(String[] args) {
		try {
			Document document = new Document(PageSize.A4);
			// RtfWriter2.getInstance(document,new
			// FileOutputStream("F:\\test.doc" ));
			document.open();
			Paragraph title = new Paragraph("你好 地球人...");
			document.add(title);
			document.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param filename
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void createPdf(String filename) throws DocumentException,
			IOException {
		// step 1：创建Document对象
		Document document = new Document();
		// step 2：获取PdfWriter实例
		PdfWriter.getInstance(document, new FileOutputStream(filename));
		// step 3：打开Document
		document.open();

		// step 4：添加内容
		document.add(new Paragraph("Hello World  my  PDF!"));

		// step 5：关闭打开的Document
		document.close();
	}

	/*
	 * pdfpath：路径 creator:作者 Title标题
	 */
	public static void createPDf(String pdfPath, String creator, String Title)
			throws DocumentException, Exception {
		// 定义一个pdf路径
		// String pdfPath1 = pdfPath;
		// 创建一个A4大小的页面
		Document document = new Document(PageSize.A4);
		// 创建一个PdfWriter实例
		PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
		// 打开document
		document.open();
		// 添加说明
		document.addAuthor("whmwg");
		document.addCreationDate();
		document.addCreator("whmwg");
		document.addKeywords("itext");
		document.addSubject("测试");
		document.addTitle("itext test");

		// 添加自定义属性
		document.addHeader("公司名称", "http://www.3wsoft.com.cn");

		// 添加内容
		document.add(new Paragraph("hello world"));
		// 换行
		document.add(Chunk.NEWLINE);

		// // 添加中文
		// BaseFont bf = BaseFont.createFont("ssfs", "UTF-8",
		// BaseFont.NOT_EMBEDDED);
		// Font font = new Font(bf, 12, Font.NORMAL, BaseColor.BLUE);
		document.add(new Paragraph("xayh"));

		// 关闭document
		document.close();
	}

	// 合并pdf 不确定数量的时候传入一个数组
	public static void coomrows(String[] pdif1) throws IOException,
			DocumentException {

		PdfReader reader1 = new PdfReader(FILE_DIR + pdif1[0]);
		PdfReader reader2 = new PdfReader(FILE_DIR + pdif1[1]);

		FileOutputStream out = new FileOutputStream(FILE_DIR + "mergePDF.pdf");

		Document document = new Document();
		PdfWriter writer = PdfWriter.getInstance(document, out);

		document.open();
		PdfContentByte cb = writer.getDirectContent();

		int totalPages = 0;
		totalPages += reader1.getNumberOfPages();
		totalPages += reader2.getNumberOfPages();

		java.util.List<PdfReader> readers = new ArrayList<PdfReader>();
		readers.add(reader1);
		readers.add(reader2);

		int pageOfCurrentReaderPDF = 0;
		Iterator<PdfReader> iteratorPDFReader = readers.iterator();

		while (iteratorPDFReader.hasNext()) {
			PdfReader pdfReader = iteratorPDFReader.next();

			while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
				document.newPage();
				pageOfCurrentReaderPDF++;
				PdfImportedPage page = writer.getImportedPage(pdfReader,
						pageOfCurrentReaderPDF);
				cb.addTemplate(page, 0, 0);
			}
			pageOfCurrentReaderPDF = 0;
		}
		out.flush();
		document.close();
		out.close();
	}

	// 合并 files :pdf的文件名。加上转化后的路径
	// String[] files = { "G:/abc/1.pdf", "G:/abc/2.pdf" , "G:/abc/3.pdf",
	// "G:/abc/4.pdf"};
	// String savepath = "G:/test3.pdf";
	// morePdfTopdf(files, savepath);
	public static void morePdfTopdf(String[] files, String savepath) {
		Document document = null;
		try {
			document = new Document(new PdfReader(files[0]).getPageSize(1));
			PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath));
			document.open();
			for (int i = 0; i < files.length; i++) {
				PdfReader reader = new PdfReader(files[i]);
				int n = reader.getNumberOfPages();// 获得总页码
				for (int j = 1; j <= n; j++) {
					document.newPage();
					PdfImportedPage page = copy.getImportedPage(reader, j);// 从当前Pdf,获取第j页
					copy.addPage(page);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} finally {
			if (document != null) {
				document.close();
			}
		}
	}

}